package com.mf.all.controller.auth;


import com.mf.all.constant.ConstantBasic;
import com.mf.all.emuns.enumsPublic.EnumPublicEnableOrDisable;
import com.mf.all.entity.dto.DtoPermission;
import com.mf.all.entity.req.publicReq.updateStatus.ReqPublicLongIdUpdateStatus;
import com.mf.all.entity.req.user.ReqUserUpdatePwd;
import com.mf.all.groups.GroupsAddInterface;
import com.mf.all.groups.GroupsAdminUpdateInterface;
import com.mf.all.groups.GroupsUpdateInterface;
import com.mf.all.result.Result;
import com.mf.all.service.ServicePermission;
import com.mf.all.utils.UtilController;
import com.mf.all.utils.UtilsEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.annotation.Resource;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;


import com.mf.all.entity.dto.DtoUser;
import com.mf.all.entity.req.user.ReqUser;
import com.mf.all.service.ServiceUser;
import com.mf.all.entity.req.search.user.SearchReqUser;

import java.util.List;

/**
 * 用户表
 *
 * @date 2025-04-02 22:36:48
 */
@RestController
@RequestMapping("user")
@Tag(name = "用户表")
public class ControllerUser {
    @Resource
    private ServiceUser Serviceuser;

    @Resource
    private ServicePermission servicePermission;


    @Operation(summary = "分页列表查询")
    @PostMapping("/page")
    @SaCheckPermission("user:search")
    public Result<Page<DtoUser>> findPage(@RequestBody SearchReqUser searchReqUser) {
        return Result.success(Serviceuser.findPageByParams(searchReqUser));
    }

    @Operation(summary = "列表查询")
    @PostMapping("/list")
    @SaCheckPermission("user:search")
    public Result<List<DtoUser>> list(@RequestBody SearchReqUser searchReqUser) {
        return Result.success(Serviceuser.findListByParams(searchReqUser));
    }

    @Operation(summary = "保存")
    @PostMapping("/save")
    @SaCheckPermission("user:save")
    public Result<Void> save(@Validated(GroupsAddInterface.class) @RequestBody ReqUser reqUser) {
        Serviceuser.save(UtilController.setBo(reqUser, DtoUser.class));
        return Result.success();
    }

    @Operation(summary = "修改")
    @PostMapping("/update")
    @SaCheckPermission("user:update")
    public Result<Void> update(@Validated(GroupsUpdateInterface.class) @RequestBody ReqUser reqUser) {
        reqUser.setUsername(null);
        Serviceuser.updateById(UtilController.setBo(reqUser, DtoUser.class));
        return Result.success();
    }


    @Operation(summary = "修改用户状态")
    @SaCheckPermission("user:status")
    @PostMapping("updateStatus")
    public Result<Void> enable(@Validated @RequestBody ReqPublicLongIdUpdateStatus reqPublicUpdateStatus) {
        Assert.state(!reqPublicUpdateStatus.getId().equals(ConstantBasic.ADMIN_ID), "不能修改管理员状态");
        UtilsEnum.existsInEnum(EnumPublicEnableOrDisable.class, reqPublicUpdateStatus.getStatus());
        final DtoUser dtoUser = UtilController.setBo(reqPublicUpdateStatus, DtoUser.class);
        Serviceuser.updateById(dtoUser);
        return Result.success();
    }

    @Operation(summary = "用户修改密码")
    @SaCheckPermission("user:updatePwd")
    @PostMapping("updatePwd")
    public Result<Void> updatePwd(@Validated(GroupsUpdateInterface.class) @RequestBody ReqUserUpdatePwd userUpdatePwd) {
        Serviceuser.updatePwd(userUpdatePwd, false);
        return Result.success();
    }

    @Operation(summary = "管理员修改密码")
    @SaCheckPermission("user:adminUpdatePwd")
    @PostMapping("adminUpdatePwd")
    public Result<Void> adminUpdatePwd(@Validated(GroupsAdminUpdateInterface.class) @RequestBody ReqUserUpdatePwd userUpdatePwd) {
        Serviceuser.updatePwd(userUpdatePwd, true);
        return Result.success();
    }

    @Operation(summary = "根据用户id查询对应权限")
    @SaCheckPermission("user:search")
    @PostMapping("listByUserId/{userId}")
    public Result<List<DtoPermission>> adminUpdatePwd(@PathVariable("userId") Long userId) {
        return Result.success(servicePermission.listByUserId(userId));
    }
}
